package gogogo.liwushuo.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gc.flashview.FlashView;
import com.gc.flashview.listener.FlashViewListener;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import gogogo.liwushuo.R;
import gogogo.liwushuo.bean.HomeBean;
import gogogo.liwushuo.bean.HomeFirstViewBean;
import gogogo.liwushuo.bean.HomeSecondViewBean;
import gogogo.liwushuo.constant.Resource;
import gogogo.liwushuo.listener.OnItemDigestClickListener;
import gogogo.liwushuo.listener.OnItemFvClickListener;
import gogogo.liwushuo.listener.OnItemIconClickListener;
import gogogo.liwushuo.listener.OnItemImageClickListener;
import gogogo.liwushuo.listener.OnItemLikeCountClickListener;
import gogogo.liwushuo.listener.OnItemLikeIconClickListener;
import gogogo.liwushuo.listener.OnItemLlayoutClickListener;
import gogogo.liwushuo.listener.OnItemNameClickListener;
import gogogo.liwushuo.listener.OnItemTitleClickListener;

/**
 * Created by chenjun on 2016/7/19.
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<HomeBean.DataBean.ItemsBean> itemsBeen;
    private List<HomeFirstViewBean.DataBean.BannersBean> firstbanners;
    private List<HomeSecondViewBean.DataBean.SecondaryBannersBean> secondbanners;
    private Context mContext;
    private int index;
    private static final int TYPE1 = 1;
    private static final int TYPE2 = 2;
    private int pos;

    public RecyclerViewAdapter(Context context, int index, List<HomeBean.DataBean.ItemsBean> beans) {
        mContext = context;
        this.index = index;
        this.itemsBeen = beans;
    }

    public RecyclerViewAdapter(Context mContext, int index, List<HomeBean.DataBean.ItemsBean> itemsBeen, List<HomeFirstViewBean.DataBean.BannersBean> firstbanners, List<HomeSecondViewBean.DataBean.SecondaryBannersBean> secondbanners) {
       this.mContext = mContext;
        this.index = index;
        this.itemsBeen = itemsBeen;
        this.firstbanners =  firstbanners;
        this.secondbanners = secondbanners;
    }

    @Override
    public int getItemViewType(int position) {
        if (index == 0) {
            if (position == 0) {
                return TYPE1;
            } else {
                return TYPE2;
            }
        }
        return TYPE2;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE1:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_home_firstview, parent, false);
                return new FirstViewHolder(view);
            case TYPE2:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_home_recycler, parent, false);
                return new MyHolder(view);
        }
        return null;
    }

    public void setList(List<HomeBean.DataBean.ItemsBean> beans) {
        this.itemsBeen = beans;
        notifyDataSetChanged();
    }

    public void setList(List<HomeBean.DataBean.ItemsBean> itemsBeen, List<HomeFirstViewBean.DataBean.BannersBean> firstbean, List<HomeSecondViewBean.DataBean.SecondaryBannersBean> secondbanners) {
        this.itemsBeen = itemsBeen;
        this.firstbanners =firstbean;
        this.secondbanners =secondbanners;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        switch (type) {
            case TYPE1:
                setItemType1((FirstViewHolder) holder, position);
                break;
            case TYPE2:
                setItemType2((MyHolder) holder, position);
                break;
        }

    }

    private void setItemType2(MyHolder holder, int position) {
        MyHolder mHolder = holder;
        HomeBean.DataBean.ItemsBean bean;
        if (index == 0){
            bean = itemsBeen.get(position-1);
        }else {
            bean =itemsBeen.get(position);
        }

        mHolder.tvCategory.setText(bean.getColumn().getCategory());
        mHolder.tvDigest.setText(bean.getTitle());
        mHolder.tvLikecCount.setText(bean.getLikes_count() + "");
        mHolder.tvName.setText(bean.getAuthor().getNickname());
        mHolder.tvTitle.setText(bean.getColumn().getTitle());
        String cover_image_url = bean.getCover_image_url();
        Picasso.with(mContext)
                .load(cover_image_url)
                .tag(cover_image_url)
                .placeholder(R.color.textcolor_unchecked)
                .error(R.color.textcolor_unchecked)
                .into(mHolder.ivImage);
        Picasso.with(mContext)
                .load(bean.getAuthor().getAvatar_url())
                .tag(cover_image_url)
                .placeholder(R.color.textcolor_unchecked)
                .error(R.color.textcolor_unchecked)
                .into(mHolder.ivIcon);
        setmHolderListener(mHolder,position);
    }

    private void setItemType1(FirstViewHolder holder, int position) {
        final FirstViewHolder fHolder = holder;
        List<String> mList = new ArrayList<>();
        for (int i = 0; i < firstbanners.size(); i++) {
            mList.add(firstbanners.get(i).getImage_url());
        }
        fHolder.fv.setImageUris(mList);
        fHolder.fv.setEffect(7);
        for (int i = 0; i < secondbanners.size(); i++) {
            pos = i;
            final ImageView iv = new ImageView(mContext);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(160, 160);
            iv.setPadding(8, 8, 8, 8);
            iv.setLayoutParams(lp);
            iv.setId(i);
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(llayoutClickListener!=null)
                        llayoutClickListener.onItemClick(pos,iv,fHolder);
                }
            });
            Picasso.with(mContext)
                    .load(secondbanners.get(i).getImage_url())
                    .into(iv);
            fHolder.secondView.addView(iv);
        }
        SimpleDateFormat format = new SimpleDateFormat("—— MM月dd日 E ——");
        fHolder.currentTime.setText(format.format(new Date(System.currentTimeMillis())));
        setfHolderLisetner(fHolder,position);
    }

    private void setmHolderListener(final MyHolder mHolder, final int position) {
        mHolder.tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (nameClickListener != null)
                nameClickListener.onItemClick(position,mHolder.tvName,mHolder);
            }
        });
        mHolder.ivLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (likeIconClickListener != null)
                likeIconClickListener.onItemClick(position,mHolder.tvLikecCount,mHolder.ivLike,mHolder);
            }
        });
        mHolder.tvLikecCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (likeClickListener != null)
                likeClickListener.onItemClick(position,mHolder.ivLike,mHolder.tvLikecCount,mHolder);
            }
        });
        mHolder.ivImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imageClickListener != null)
                imageClickListener.onItemClick(position,mHolder.ivImage,mHolder);
            }
        });
        mHolder.tvTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (titleClickListener != null)
                    titleClickListener.onItemClick(position,mHolder.tvTitle,mHolder);
            }
        });
        mHolder.ivIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (iconClickListener != null)
                    iconClickListener.onItemClick(position,mHolder.ivIcon,mHolder);
            }
        });
        mHolder.tvDigest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (digestClickListener != null)
                    digestClickListener.onItemClick(position,mHolder.tvDigest,mHolder);
            }
        });
    }

    private void setfHolderLisetner(final FirstViewHolder fHolder, int position) {
        fHolder.fv.setOnPageClickListener(new FlashViewListener() {
            @Override
            public void onClick(int position) {
                if (fvClickListener != null)
                    fvClickListener.onItemClick(position,fHolder.fv,fHolder);
            }
        });
//        for (int i = 0; i < secondbanners.size(); i++) {
//            pos = i;
//            fHolder.secondView.findViewById(pos).setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if(llayoutClickListener!=null)
//                        llayoutClickListener.onItemClick(pos,fHolder.secondView,fHolder);
//                }
//            });
//        }
    }

    @Override
    public int getItemCount() {
        if (index != 0) {
            return itemsBeen == null ? 0 : itemsBeen.size();
        } else {
            return itemsBeen == null ? 0 : itemsBeen.size() + 1;
        }

    }

    public class MyHolder extends RecyclerView.ViewHolder {
        private TextView tvCategory, tvTitle, tvName, tvDigest, tvLikecCount;
        private ImageView ivIcon, ivImage,ivLike;
        public boolean isLike;

        public MyHolder(View v) {
            super(v);
            tvCategory = (TextView) v.findViewById(R.id.tv_category_recycler);
            tvTitle = (TextView) v.findViewById(R.id.tv_title_recycler);
            tvName = (TextView) v.findViewById(R.id.tv_author_recycler);
            tvDigest = (TextView) v.findViewById(R.id.tv_digest_recycler);
            tvLikecCount = (TextView) v.findViewById(R.id.tv_count_recycler);
            ivIcon = (ImageView) v.findViewById(R.id.iv_icon_recycler);
            ivImage = (ImageView) v.findViewById(R.id.iv_image_recycler);
            ivLike = (ImageView) v.findViewById(R.id.iv_like_recycler);
        }
    }

    public class FirstViewHolder extends RecyclerView.ViewHolder {

        private FlashView fv;
        private LinearLayout secondView;
        private TextView currentTime;

        public FirstViewHolder(View v) {
            super(v);
            fv = (FlashView) v.findViewById(R.id.fv);
            secondView = (LinearLayout) v.findViewById(R.id.item_secondview);
            currentTime = (TextView) v.findViewById(R.id.item_home_currenttime);
        }
    }


    private OnItemNameClickListener nameClickListener;
    private OnItemTitleClickListener titleClickListener;
    private OnItemDigestClickListener digestClickListener;
    private OnItemLikeCountClickListener likeClickListener;
    private OnItemIconClickListener iconClickListener;
    private OnItemLikeIconClickListener likeIconClickListener;
    private OnItemImageClickListener imageClickListener;
    private OnItemFvClickListener fvClickListener;
    private OnItemLlayoutClickListener llayoutClickListener;
    public void setOnItemNameClickListener(OnItemNameClickListener listener){
        this.nameClickListener = listener;
    }

    public void setOnItemTitltClickListener(OnItemTitleClickListener listener){
        this.titleClickListener = listener;
    }
    public void setOnItemDigestClickListener(OnItemDigestClickListener listener){
        this.digestClickListener = listener;
    }
    public void setOnItemLikeCountClickListener(OnItemLikeCountClickListener listener){
        this.likeClickListener = listener;
    }
    public void setOnItemIconClickListener(OnItemIconClickListener listener){
        this.iconClickListener = listener;
    }
    public void setOnItemLikeIconClickListener(OnItemLikeIconClickListener listener){
        this.likeIconClickListener = listener;
    }
    public void setOnItemImageClickListener(OnItemImageClickListener listener){
        this.imageClickListener = listener;
    }
    public void setOnItemFvClickListener(OnItemFvClickListener listener){
        this.fvClickListener = listener;
    }
    public void setOnItemLlayoutClickListener(OnItemLlayoutClickListener listener){
        this.llayoutClickListener = listener;
    }
}
